$(document).ready(function () {
	var userInfo;
	fillFamilyInfo();

    $("#update-btn").on("click", function() {
        if (updatePersonalInfoValidate()) {
            var data = { StudentName: $("#student-name").val(), StudentSex: $("#student-sex").val(), FatherName: $("#father-name").val(), Mobile: $("#phone-number").val(), };
            var url = "../Handle/FamilyHandlerService.ashx?method=modify_One_Family_Info";
            $.ajax({
                type: "POST",
                data: data,
                url: url,
                dataType: "json",
                success: function (data) {
                    if (1 == data.MessageType) {
                    	alert(data.MessageContent);
                        // $("#dialog-modal-label").text(data.MessageContent);
                        // $("#DialogModal").modal("show");
                    	logOut();
                    } else {
                    	// alert(data.MessageContent);
                        $("#dialog-modal-label").text(data.MessageContent);
                        $("#DialogModal").modal("show");
                    }
                }
            });
        }
    });
});


function fillFamilyInfo() {
    $.ajax({
        type: "GET",
        url: "../Handle/FamilyHandlerService.ashx?method=Get_One_Family_Info",
        dataType: "json",
        success: function (data) {
        	// console.log(data.rows[0]);
        	$("#student-name").val(data.rows[0]["Sname"]);
        	$("#student-sex").val(data.rows[0]["Sex"]);
        	$("#father-name").val(data.rows[0]["Fname"]);
        	$("#phone-number").val(data.rows[0]["Mobile"]);
        }
    });
}

function updatePersonalInfoValidate() {
    return $("#update-user-info-form").validate({
        errorClass: "warning",
        rules: {
        	studentName: {
                required: true,
                minlength: 2,
                maxlength: 4,
        	},
        	studentSex: {
                required: true,

        	},
        	fatherName: {
                required: true,
                minlength: 2,
                maxlength: 4, 
        	},
            phoneNumber: {
                required: true,
                digits:true,
                minlength: 11,
                maxlength: 11,
            },
        },
        messages: {
        	studentName: {
                required: "请输入学生姓名",
                minlength: "姓名不少于2个字",
                maxlength: "姓名多少于4个字",
        	},
        	studentSex: {
                required: "请输入性别",
        	},
        	fatherName: {
                required: "请输入家长姓名",
                minlength: "姓名不少于2个字",
                maxlength: "姓名多少于4个字", 
        	},
            phoneNumber: {
                required: "请输入电话号码",
                digits: "电话号码是数字",
                minlength: "电话号码有11位数字",
                maxlength: "电话号码有11位数字"
            },
        }
    }).form();
}
